//异常拦截器
import { HttpException, ExceptionFilter, Catch, ArgumentsHost } from "@nestjs/common";
import { Request, Response } from "express";

@Catch(HttpException)
export class HttpFilter implements ExceptionFilter {
    catch(exception: HttpException, host: ArgumentsHost) {
        const cxt = host.switchToHttp();
        const request = cxt.getRequest<Request>();
        const response = cxt.getResponse<Response>();

        const status = exception.getStatus();

        response.status(status).json({
            data: exception.getResponse(),
            time: new Date().getTime(),
            success: false,
            path: request.url,
            status
        })
    }
}